package com.huawei.appgallery.festivalanimation.particle;

import android.animation.ValueAnimator;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.Log;
import android.view.ViewGroup;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;
import com.huawei.appgallery.festivalanimation.particle.initializers.AccelerationInitializer;
import com.huawei.appgallery.festivalanimation.particle.initializers.AlphaInitializer;
import com.huawei.appgallery.festivalanimation.particle.initializers.IParticleInitializer;
import com.huawei.appgallery.festivalanimation.particle.initializers.ParticleInitializer;
import com.huawei.appgallery.festivalanimation.particle.initializers.RotationInitializer;
import com.huawei.appgallery.festivalanimation.particle.initializers.RotationSpeedInitializer;
import com.huawei.appgallery.festivalanimation.particle.initializers.ScaleInitializer;
import com.huawei.appgallery.festivalanimation.particle.initializers.SpeedModuleAndRangeInitializer;
import com.huawei.appgallery.festivalanimation.particle.modifiers.IParticleModifier;
import com.huawei.appgallery.festivalanimation.particle.modifiers.OpacityModifier;
import com.huawei.appgallery.festivalanimation.particle.modifiers.ParticleModifier;
import com.huawei.appgallery.festivalanimation.particle.modifiers.SizeModifier;
import com.huawei.appgallery.festivalanimation.particle.utils.RandomUtil;
import com.huawei.hwphy2d.physical.util.DeviceParameters;
import java.lang.ref.WeakReference;
import java.security.InvalidParameterException;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Iterator;
import o.bqq;

public class ParticleSystem {
    private static long TIMER_TASK_INTERVAL = 33;
    private HandlerThread handlerThread;
    private int mActivatedParticles;
    private final ArrayList<Particle> mActiveParticles;
    private ValueAnimator mAnimator;
    private long mCurrentTime;
    private float mDpToPxScale;
    private ParticleView mDrawingView;
    private int mEmitterXMax;
    private int mEmitterXMin;
    private int mEmitterYMax;
    private int mEmitterYMin;
    private long mEmittingTime;
    private Path mEmmiterPath;
    private Handler mHandler;
    private int mMaxParticles;
    private int[] mParentLocation;
    private ViewGroup mParentView;
    private ParticleInitializer mParticleInitializer;
    private ParticleModifier mParticleModifier;
    private ArrayList<Particle> mParticles;
    private float mParticlesPerMillisecond;
    private boolean mPathEmitter;
    private PathMeasure mPathMeasure;
    private long mTimeToLive;
    private ParticleFireUpdateTask mUpdateFireTask;
    private ParticleUpdateTask mUpdateTask;

    static class ParticleFireUpdateTask implements Runnable {
        private final Handler mHandler;
        private final WeakReference<ParticleSystem> mPs;

        public ParticleFireUpdateTask(ParticleSystem particleSystem, Handler handler) {
            this.mPs = new WeakReference(particleSystem);
            this.mHandler = handler;
        }

        public void run() {
            if (this.mPs.get() != null) {
                ParticleSystem particleSystem = (ParticleSystem) this.mPs.get();
                if (particleSystem != null) {
                    particleSystem.onUpdateFireWork(particleSystem.mCurrentTime);
                    particleSystem.mCurrentTime = particleSystem.mCurrentTime + ParticleSystem.TIMER_TASK_INTERVAL;
                    if (this.mHandler != null) {
                        this.mHandler.postDelayed(this, ParticleSystem.TIMER_TASK_INTERVAL);
                    }
                }
            }
        }
    }

    static class ParticleUpdateTask implements Runnable {
        private final Handler mHandler;
        private final WeakReference<ParticleSystem> mPs;

        public ParticleUpdateTask(ParticleSystem particleSystem, Handler handler) {
            this.mPs = new WeakReference(particleSystem);
            this.mHandler = handler;
        }

        public void run() {
            if (this.mPs.get() != null) {
                ParticleSystem particleSystem = (ParticleSystem) this.mPs.get();
                if (particleSystem != null) {
                    particleSystem.onUpdate(particleSystem.mCurrentTime);
                    particleSystem.mCurrentTime = particleSystem.mCurrentTime + ParticleSystem.TIMER_TASK_INTERVAL;
                    if (this.mHandler != null) {
                        this.mHandler.postDelayed(this, ParticleSystem.TIMER_TASK_INTERVAL);
                    }
                }
            }
        }
    }

    private ParticleSystem(ViewGroup viewGroup, int i, long j) {
        this.mActiveParticles = new ArrayList();
        this.mCurrentTime = 0;
        this.mPathEmitter = false;
        this.mEmmiterPath = null;
        this.mPathMeasure = null;
        if (viewGroup == null) {
            throw new InvalidParameterException("parentview should be NonNull or parentViewId should be a valid Id");
        }
        initWorkerThread();
        this.mHandler = new Handler(this.handlerThread.getLooper());
        this.mUpdateTask = new ParticleUpdateTask(this, this.mHandler);
        this.mUpdateFireTask = new ParticleFireUpdateTask(this, this.mHandler);
        this.mParentLocation = new int[2];
        setParentViewGroup(viewGroup);
        this.mParticleModifier = new ParticleModifier();
        this.mParticleInitializer = new ParticleInitializer();
        this.mMaxParticles = i;
        this.mParticles = new ArrayList();
        this.mTimeToLive = j;
        this.mDpToPxScale = viewGroup.getContext().getResources().getDisplayMetrics().xdpi / 160.0f;
    }

    public ParticleSystem(ViewGroup viewGroup, int i, Drawable drawable, long j, boolean z) {
        int i2 = 0;
        this(viewGroup, i, j);
        if (drawable == null) {
            throw new InvalidParameterException("drawable should be NonNull");
        }
        Bitmap bitmap;
        if (drawable instanceof BitmapDrawable) {
            bitmap = ((BitmapDrawable) drawable).getBitmap();
        } else {
            bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
            drawable.draw(canvas);
            canvas.setBitmap(null);
        }
        while (i2 < this.mMaxParticles) {
            this.mParticles.add(new Particle(bitmap, z));
            i2++;
        }
    }

    protected void initWorkerThread() {
        this.handlerThread = new HandlerThread("Handler Thread");
        this.handlerThread.start();
    }

    public ParticleSystem(ViewGroup viewGroup, int i, long j, Boolean bool, Drawable... drawableArr) {
        this(viewGroup, i, j);
        SecureRandom secureRandom = new SecureRandom();
        for (int i2 = 0; i2 < this.mMaxParticles; i2++) {
            Drawable drawable = drawableArr[secureRandom.nextInt(drawableArr.length)];
            if (drawable == null) {
                throw new InvalidParameterException("drawable should be NonNull");
            }
            Bitmap bitmap;
            if (drawable instanceof BitmapDrawable) {
                bitmap = ((BitmapDrawable) drawable).getBitmap();
            } else {
                Bitmap createBitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Config.ARGB_8888);
                Canvas canvas = new Canvas(createBitmap);
                drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
                drawable.draw(canvas);
                canvas.setBitmap(null);
                bitmap = createBitmap;
            }
            this.mParticles.add(new Particle(bitmap, bool.booleanValue()));
        }
    }

    public ParticleSystem(Activity activity, int i, int i2, long j) {
        this(activity, i, activity.getResources().getDrawable(i2), j, 16908290, false);
    }

    public ParticleSystem(Activity activity, int i, int i2, long j, int i3) {
        this(activity, i, activity.getResources().getDrawable(i2), j, i3, false);
    }

    public ParticleSystem(Activity activity, int i, Drawable drawable, long j) {
        this(activity, i, drawable, j, 16908290, false);
    }

    public ParticleSystem(Activity activity, int i, Drawable drawable, long j, int i2, boolean z) {
        this((ViewGroup) activity.findViewById(i2), i, drawable, j, z);
    }

    public float dpToPx(float f) {
        return this.mDpToPxScale * f;
    }

    public ParticleSystem(Activity activity, int i, Bitmap bitmap, long j) {
        this(activity, i, bitmap, j, 16908290);
    }

    public ParticleSystem setInitialRotationRange(int i, int i2) {
        addInitializer(new RotationInitializer(i, i2));
        return this;
    }

    public ParticleSystem setRotationSpeed(float f) {
        addInitializer(new RotationSpeedInitializer(f, f));
        return this;
    }

    public ParticleSystem setRotationSpeedRange(float f, float f2) {
        Log.d("wenjie2", "minRotationSpeed = " + f + ", maxRotationSpeed = " + f2);
        addInitializer(new RotationSpeedInitializer(f, f2));
        return this;
    }

    public ParticleSystem(Activity activity, int i, Bitmap bitmap, long j, int i2) {
        this((ViewGroup) activity.findViewById(i2), i, j);
        if (bitmap == null) {
            throw new InvalidParameterException("bitmap should be NonNull");
        }
        for (int i3 = 0; i3 < this.mMaxParticles; i3++) {
            this.mParticles.add(new Particle(bitmap));
        }
    }

    public static void setFPS(double d) {
        TIMER_TASK_INTERVAL = Math.round(1000.0d / d);
    }

    public ParticleSystem setSpeedRange(float f, float f2, int i, int i2) {
        addInitializer(new SpeedModuleAndRangeInitializer(dpToPx(f), dpToPx(f2), i, i2));
        return this;
    }

    public ParticleSystem setSpeedRange(float f, float f2) {
        addInitializer(new SpeedModuleAndRangeInitializer(dpToPx(f), dpToPx(f2), 0, 360));
        return this;
    }

    public ParticleSystem setScaleRange(float f, float f2) {
        addInitializer(new ScaleInitializer(f, f2));
        return this;
    }

    public ParticleSystem setAlphaRange(int i, int i2) {
        if (i > i2 || i < 0 || i2 < 0 || i > 255 || i2 > 255) {
            throw new InvalidParameterException("assert(minAlpha <= maxAlpha) minAlpha,maxAlpha[0,255]");
        }
        addInitializer(new AlphaInitializer(i, i2));
        return this;
    }

    public ParticleSystem setAcceleration(float f, int i, int i2) {
        addInitializer(new AccelerationInitializer(f, f, i, i2));
        return this;
    }

    private ParticleSystem addInitializer(IParticleInitializer iParticleInitializer) {
        if (this.mParticleInitializer == null) {
            this.mParticleInitializer = new ParticleInitializer();
        }
        this.mParticleInitializer.addInitializer(iParticleInitializer);
        return this;
    }

    public ParticleSystem setParentViewGroup(ViewGroup viewGroup) {
        this.mParentView = viewGroup;
        if (this.mParentView != null) {
            this.mParentView.getLocationInWindow(this.mParentLocation);
        }
        return this;
    }

    public ParticleSystem setOpacityOverLife(int i, int i2, long j, long j2, Interpolator interpolator) {
        addModifier(new OpacityModifier(i, i2, j, j2, interpolator));
        return this;
    }

    public ParticleSystem setSizeOverLife(float f, float f2, long j, long j2, Interpolator interpolator) {
        addModifier(new SizeModifier(f, f2, j, j2, interpolator));
        return this;
    }

    public ParticleSystem setFadeOut(long j, Interpolator interpolator) {
        addModifier(new OpacityModifier(-1, 0, this.mTimeToLive - j, this.mTimeToLive, interpolator));
        return this;
    }

    public ParticleSystem setFadeOut(long j) {
        return setFadeOut(j, new LinearInterpolator());
    }

    private ParticleSystem addModifier(IParticleModifier iParticleModifier) {
        if (this.mParticleModifier == null) {
            this.mParticleModifier = new ParticleModifier();
        }
        this.mParticleModifier.addModifier(iParticleModifier);
        return this;
    }

    public void oneShot(Point point, int i, int i2) {
        oneShot(point, i, new LinearInterpolator(), i2);
    }

    public void oneShot(Point point, int i, Interpolator interpolator, int i2) {
        configureEmitter(point);
        this.mActivatedParticles = 0;
        this.mEmittingTime = this.mTimeToLive;
        this.mDrawingView = new ParticleView(this.mParentView.getContext());
        this.mParentView.addView(this.mDrawingView);
        this.mDrawingView.setParticles(this.mActiveParticles);
        this.mHandler.postDelayed(this.mUpdateFireTask, (long) i2);
    }

    private void startAnimator(Interpolator interpolator, long j) {
        this.mAnimator = ValueAnimator.ofInt(new int[]{0, (int) j});
        this.mAnimator.setDuration(j);
        this.mAnimator.addUpdateListener(new 1(this));
        this.mAnimator.addListener(new 2(this));
        this.mAnimator.setInterpolator(interpolator);
        this.mAnimator.start();
    }

    private void configureEmitter(Point point) {
        int i = point.x - this.mParentLocation[0];
        this.mEmitterXMax = i;
        this.mEmitterXMin = i;
        i = point.y - this.mParentLocation[1];
        this.mEmitterYMax = i;
        this.mEmitterYMin = i;
    }

    public void emit(Rect rect, int i) {
        configureEmitter(rect);
        startEmitting(i);
    }

    public void emit(Path path, int i) {
        configureEmitter(path);
        startEmitting(i);
    }

    private void startEmitting(int i) {
        this.mActivatedParticles = 0;
        this.mParticlesPerMillisecond = ((float) i) / 1000.0f;
        this.mDrawingView = new ParticleView(this.mParentView.getContext());
        this.mParentView.addView(this.mDrawingView);
        this.mEmittingTime = 12000;
        this.mDrawingView.setParticles(this.mActiveParticles);
        this.mHandler.postDelayed(this.mUpdateTask, TIMER_TASK_INTERVAL);
    }

    private void configureEmitter(Rect rect) {
        this.mEmitterXMin = rect.left - this.mParentLocation[0];
        this.mEmitterXMax = this.mEmitterXMin + rect.width();
        this.mEmitterYMin = rect.top - this.mParentLocation[1];
        this.mEmitterYMax = this.mEmitterYMin + rect.height();
    }

    private void configureEmitter(Path path) {
        this.mPathEmitter = true;
        this.mEmmiterPath = path;
        this.mPathMeasure = new PathMeasure(this.mEmmiterPath, true);
    }

    private void activateParticle(long j) {
        Particle particle = (Particle) this.mParticles.remove(0);
        particle.init();
        this.mParticleInitializer.initParticle(particle);
        if (!this.mPathEmitter || this.mPathMeasure == null) {
            particle.configure(this.mTimeToLive, (float) getFromRange(this.mEmitterXMin, this.mEmitterXMax), (float) getFromRange(this.mEmitterYMin, this.mEmitterYMax));
        } else {
            float[] fromRange = getFromRange(0.0f, this.mPathMeasure.getLength());
            particle.configure(this.mTimeToLive, (float) ((int) fromRange[0]), (float) ((int) fromRange[1]));
        }
        particle.activate(j, this.mParticleModifier);
        this.mActiveParticles.add(particle);
        this.mActivatedParticles++;
    }

    private int getFromRange(int i, int i2) {
        if (i == i2) {
            return i;
        }
        if (i < i2) {
            return i + RandomUtil.nextInt(i2 - i);
        }
        return RandomUtil.nextInt(i - i2) + i2;
    }

    private float[] getFromRange(float f, float f2) {
        float nextFloat;
        if (f <= f2) {
            nextFloat = RandomUtil.nextFloat(f2 - f) + f;
        } else {
            nextFloat = RandomUtil.nextFloat(f - f2) + f2;
        }
        if (this.mPathMeasure == null) {
            this.mPathMeasure = new PathMeasure(this.mEmmiterPath, true);
        }
        r1 = new float[2];
        this.mPathMeasure.getPosTan(nextFloat, r1, null);
        r1[0] = r1[0] - ((float) this.mParentLocation[0]);
        r1[1] = r1[1] - ((float) this.mParentLocation[1]);
        return r1;
    }

    private void onUpdate(long j) {
        while (true) {
            if (((this.mEmittingTime <= 0 || j >= this.mEmittingTime) && this.mEmittingTime != -1) || this.mParticles.isEmpty() || ((float) this.mActivatedParticles) >= this.mParticlesPerMillisecond * ((float) j)) {
            } else {
                activateParticle(j);
            }
        }
        synchronized (this.mActiveParticles) {
            Iterator it = this.mActiveParticles.iterator();
            while (it.hasNext()) {
                Particle particle = (Particle) it.next();
                if (!particle.update(j)) {
                    it.remove();
                    this.mParticles.add(particle);
                }
            }
        }
        if (this.mDrawingView != null) {
            this.mDrawingView.postInvalidate();
        }
    }

    private double getRandom() {
        double random = Math.random();
        if (random < 0.15d) {
            return random + 0.15d;
        }
        if (random > 0.85d) {
            return random - 0.15d;
        }
        return random;
    }

    private void onUpdateFireWork(long j) {
        Point point = new Point();
        point.x = (int) (((double) DeviceParameters.getScreenWidth(this.mParentView.getContext())) * getRandom());
        point.y = (int) ((((double) DeviceParameters.getScreenHeight(this.mParentView.getContext())) * getRandom()) * 0.4d);
        configureEmitter(point);
        while (!this.mParticles.isEmpty() && this.mActivatedParticles < 70 && j < 9 * this.mEmittingTime) {
            activateParticle(j);
        }
        synchronized (this.mActiveParticles) {
            Iterator it = this.mActiveParticles.iterator();
            while (it.hasNext()) {
                Particle particle = (Particle) it.next();
                if (!particle.update(j)) {
                    it.remove();
                    this.mActivatedParticles--;
                    this.mParticles.add(particle);
                }
            }
        }
        if (this.mDrawingView != null) {
            this.mDrawingView.postInvalidate();
        }
    }

    private void cleanupAnimation() {
        this.mParentView.removeView(this.mDrawingView);
        this.mDrawingView = null;
        this.mParentView.postInvalidate();
        this.mParticles.addAll(this.mActiveParticles);
    }

    public void stopEmitting() {
        this.mEmittingTime = this.mCurrentTime;
    }

    public void cancel() {
        if (this.mHandler != null) {
            this.mHandler.removeCallbacks(this.mUpdateTask);
            cleanupAnimation();
            if (this.handlerThread != null) {
                this.handlerThread.quit();
                this.handlerThread = null;
            }
            bqq.ॱ.ˋ("ParticleSystem", "cancel");
        }
    }
}
